﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CherryTomato;
using CherryTomato.Entities;
using System.Collections;

namespace LikeUs.MovieServices
{
    public class TomatoApiService : IExteranlApiServices
    {
        private string m_apiKey;
        private Tomato m_tomatoService;
        /// <summary>
        /// The number of results per page when calling FindMovieByQuery method
        /// The Maximum Value is 50!!!
        /// </summary>
        private int PER_PAGE = 50;

        public TomatoApiService()
        {
            m_apiKey = "ym4unvumxvwdkzzwp9cr45ed";
            m_tomatoService = new Tomato(m_apiKey);
        }

        public List<LikeUsMovie> GetBoxOfficeList()
        {
            List<LikeUsMovie> resultList = new List<LikeUsMovie>();
            try
            {                
                MovieSearchResults msr = m_tomatoService.FindBoxOfficeList();                
                foreach (Movie movie in msr)
                {
                    resultList.Add(ConvertMovieToLikeUs(movie));
                }                
            }
            catch (Exception)
            {
            }
            return resultList;
        }

        public LikeUsMovie GetMovieById(int movieId)
        {
            try
            {
                return ConvertMovieToLikeUs(m_tomatoService.FindMovieById(movieId));
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<LikeUsMovie> SearchMovie(string query)
        {
            List<LikeUsMovie> resultList = new List<LikeUsMovie>();
            try
            {
                MovieSearchResults msr = m_tomatoService.FindMovieByQuery(query, PER_PAGE);
                foreach (Movie movie in msr)
                {
                    resultList.Add(ConvertMovieToLikeUs(movie));
                }
                
            }
            catch (Exception)
            {
            }
            return resultList;
        }

        public List<LikeUsMovie> GetMovieCollection(ICollection<int> movieIds)
        {
            List<LikeUsMovie> resultList = new List<LikeUsMovie>();
            try
            {
                List<Movie> msr = m_tomatoService.FindMovieCollection((IEnumerable<int>)movieIds);
                foreach (Movie movie in msr)
                {
                    resultList.Add(ConvertMovieToLikeUs(movie));
                }
            }
            catch (Exception)
            {
            }
            return resultList;
        }

        public List<LikeUsMovie> GetMoviesInTheaterList()
        {
            List<LikeUsMovie> resultList = new List<LikeUsMovie>();
            try
            {
                MovieSearchResults msr = m_tomatoService.FindMoviesInTheaterList();
                foreach (Movie movie in msr)
                {
                    resultList.Add(ConvertMovieToLikeUs(movie));
                }
            }
            catch (Exception)
            {
            }
            return resultList;
        }

        public List<LikeUsMovie> GetOpeningMoviesList()
        {
            List<LikeUsMovie> resultList = new List<LikeUsMovie>();
            try
            {
                MovieSearchResults msr = m_tomatoService.FindOpeningMoviesList();
                foreach (Movie movie in msr)
                {
                    resultList.Add(ConvertMovieToLikeUs(movie));
                }
            }
            catch (Exception)
            {
            }
            return resultList;
        }

        public List<LikeUsMovie> GetUpcomingMoviesList()
        {
            List<LikeUsMovie> resultList = new List<LikeUsMovie>();
            try
            {
                MovieSearchResults msr = m_tomatoService.FindUpcomingMoviesList();
                foreach (Movie movie in msr)
                {
                    resultList.Add(ConvertMovieToLikeUs(movie));
                }
            }
            catch (Exception)
            {
            }
            return resultList;
        }

        private LikeUsMovie ConvertMovieToLikeUs(Movie movie)
        {
            LikeUsMovie likeusMovie = new LikeUsMovie();
            likeusMovie.Id = movie.RottenTomatoesId;
            likeusMovie.Title = movie.Title;
            likeusMovie.Year = movie.Year;
            likeusMovie.Synopsis = movie.Synopsis;
            if (movie.Runtime.HasValue)
            {
                likeusMovie.MovieLength = movie.Runtime.Value; 
            }
            bool first = true;
            foreach (string genre in movie.Genres)
            {
                if (first)
                {
                    likeusMovie.Genre = genre;
                    first = false;
                }
                else
                {
                    likeusMovie.Genre += ", " + genre;
                }
            }
            foreach (ReleaseDate release in movie.ReleaseDates)
            {
                if (release.Type.Equals("theater"))
                {
                    likeusMovie.ReleaseDate = release.Date;
                }
            }
            likeusMovie.Cast = new List<string>();
            foreach (CastMember castMember in movie.Cast)
            {
                string member = castMember.Name;
                first = true;
                foreach (string character in castMember.Characters)
                {
                    if (first)
                    {
                        member += ": " + character;
                        first = false;
                    }
                    else
                    {
                        member += ", " + character;
                    }
                }
                likeusMovie.Cast.Add(member);
            }

            likeusMovie.Directors = movie.Directors;
            likeusMovie.MPAARating = movie.MpaaRating;

            foreach (Poster poster in movie.Posters)
            {
                if (poster.Type.Equals("thumbnail"))
                {
                    likeusMovie.PosterSmall = poster.Url;
                }
                if (poster.Type.Equals("original"))
                {
                    likeusMovie.PosterLarge = poster.Url;
                }               
            }

            foreach (Rating rating in movie.Ratings)
            {
                if (rating.Type.Equals("critics_score"))
                {
                    likeusMovie.CriticsRating = rating.Score;
                }
            }

            return likeusMovie;
        }
    }
}
